package com.yanqu.road.dao.impl.cross;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.rank.cross.CrossMonthRankUploadOver;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.mina.util.ConcurrentHashSet;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossMonthRankUploadOverDaoImpl extends TempDataObjectDao<CrossMonthRankUploadOver> {


    public Map<Integer, CrossMonthRankUploadOver> getUploadOverServerIds(int month){
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_c_month_rank_uploadover where monthId = ?";
        Map<Integer, CrossMonthRankUploadOver> dataMap = new ConcurrentHashMap<>();
        try {
            int i = 1;
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(i++, month);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossMonthRankUploadOver data = parse(rs);
                dataMap.put(month, data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(CrossMonthRankUploadOver data) {
        PreparedStatement call = null;
        String sql = "REPLACE INTO t_c_month_rank_uploadover(monthId,serverIds,rankNumMap,rankTimeMap) values (?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getMonth());
            call.setString(i++, JSONObject.toJSONString(data.getServerIds()));
            call.setString(i++, JSONObject.toJSONString(data.getRankNumMap()));
            call.setString(i++, JSONObject.toJSONString(data.getRankTimeMap()));
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(CrossMonthRankUploadOver data) {
        PreparedStatement call = null;
        String sql = "update t_c_month_rank_uploadover set serverIds=?,rankNumMap=?,rankTimeMap=? where monthId=?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;

            call.setString(i++, JSONObject.toJSONString(data.getServerIds()));
            call.setString(i++, JSONObject.toJSONString(data.getRankNumMap()));
            call.setString(i++, JSONObject.toJSONString(data.getRankTimeMap()));
            call.setInt(i++, data.getMonth());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected CrossMonthRankUploadOver parseData(ResultSet rs) throws SQLException {
        CrossMonthRankUploadOver data = new CrossMonthRankUploadOver();
        data.setMonth(rs.getInt("monthId"));
        String serverIds = rs.getString("serverIds");
        if (!StringUtils.isNullOrEmpty(serverIds)) {
            data.setServerIds(new ConcurrentHashSet<>(JSONObject.parseObject(serverIds, new TypeReference<List<Long>>() {
            })));
        }

        String rankNumStr = rs.getString("rankNumMap");
        if (!StringUtils.isNullOrEmpty(rankNumStr)) {
            data.setRankNumMap(JSONObject.parseObject(rankNumStr, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {
            }));
        }

        String rankTimeStr = rs.getString("rankTimeMap");
        if (!StringUtils.isNullOrEmpty(rankTimeStr)) {
            data.setRankTimeMap(JSONObject.parseObject(rankTimeStr, new TypeReference<ConcurrentHashMap<Integer, Long>>() {
            }));
        }
        return data;
    }
}
